package com.mf.interfaceTest;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.mf.MyApplication;
import com.mf.page.Actions;
import com.mf.mpos.pub.CommEnum;
import com.mf.mpos.pub.Controler;
import com.mf.mpos.pub.result.*;
import com.mf.mpos.pub.EmvTagDef;


public class BalanceQuery {
	
	static boolean inputpin()
	{
		Actions.ShowUnBlockMsg("提示", "请输入密码", "", null);
		/*
		InputPinResult inputpinResult = Controler.InputPin((byte)8, (byte)60, "123456789012345678");
		if(!inputpinResult.commResult.equals(CommEnum.COMMRET.NOERROR))
		{
			Actions.ShowBlockMsg("提示", "密码输入失败", "", "确定");
			return false;
		}
		*/
		CalPinResult calpinresult = Controler.CalPin( new byte[]{0x31,0x32,0x33,0x34,0x35,0x36}, "123456789012345678");
		if(!calpinresult.commResult.equals(CommEnum.COMMRET.NOERROR))
		{
			Actions.ShowBlockMsg("提示", "密码计算失败", "", "确定");
			return false;
		}
		return true;
	}
	
	public static void proc()
	{
		//POS未连接，连接POS
		if(!Controler.posConnected())
		{
			//Actions.ShowUnBlockMsg("提示", "正在连接POS", "", null);
			//ConnectPosResult conResult = Controler.connectPos("17:78:04:00:00:00");
			Actions.ShowBluetoothSetPage();
			
			
		}
		if(!Controler.posConnected()){			
			Actions.ShowBlockMsg("提示", "POS连接失败", "", "确定");	
			return;
		}
		
		//打开读卡器
		Actions.ShowUnBlockMsg("提示", "正在打开读卡器", "", null);
		OpenCardReaderResult result = Controler.OpenCardReader("余额查询", 0, 60, CommEnum.OPENCARDTYPE.COMBINED, "");
		
		if(!result.commResult.equals(CommEnum.COMMRET.NOERROR))
		{
			Actions.ShowBlockMsg("提示", "读卡器打开失败", "", "确定");
			return;
		}
		
		//根据刷卡类型执行不同操作
		if(result.cardType.equals(CommEnum.SWIPECARDTYPE.MAGCARD))
		{
			//用户刷磁条卡，读取磁条卡信息
			Actions.ShowUnBlockMsg("提示", "正在读取磁条卡信息", "", null);
			ReadMagcardResult readMagResult = Controler.ReadMagcard(CommEnum.READCARDTRACK.COMBINED,CommEnum.PANMASK.NOMASK);
			
			if(!readMagResult.commResult.equals(CommEnum.COMMRET.NOERROR))
			{
				Actions.ShowBlockMsg("提示", "读取磁条卡信息失败", "", "确定");
				return;
			}
			
			//输入密码
			if( !inputpin() ) return;
			
			byte[] byte8583 = new byte[512];
			int actual8583Len = 0;
			/*
			 * 根据readMagResult，inputpinResult的数据组8583包，此处省略，假定为byte8583
			 */
			
			Actions.ShowUnBlockMsg("提示", "正在计算mac", "", null);
			CalMacResult macResult = Controler.CalMac(CommEnum.MACALG.EBC, byte8583,actual8583Len);
			if(!macResult.commResult.equals(CommEnum.COMMRET.NOERROR))
			{
				Actions.ShowBlockMsg("提示", "mac计算错误", "", "确定");
				return;
			}
			/*
			 * 与POSP通讯，此处省略
			 */
			
			Actions.ShowBlockMsg("提示", "卡片余额为：1000", "", "确定");
			
		}
		else if(result.cardType.equals(CommEnum.SWIPECARDTYPE.ICCARD))
		{
			//用户插入IC
			
			Actions.ShowUnBlockMsg("提示", "正在读取IC卡信息", "", null);
			StartEmvResult resultStartEmv = Controler.StartEmv(1000,0,CommEnum.TRANSTYPE.FUNC_SALE,CommEnum.ECASHPERMIT.FORBIT, CommEnum.EMVEXECTYPE.FULL, CommEnum.FORCEONLINE.YES);
			
			if(!resultStartEmv.commResult.equals(CommEnum.COMMRET.NOERROR)
				|| resultStartEmv.execResult.equals(CommEnum.STARTEMVRESULT.REJECT)
				|| resultStartEmv.execResult.equals(CommEnum.STARTEMVRESULT.FAIL))
			{
				Actions.ShowBlockMsg("提示", "IC卡处理失败", "", "确定");
				return;
			}
			
			//要求输入联机PIN
			if(resultStartEmv.onlinePinInput)
			{
				//输入密码
				if( !inputpin() ) return;
			}
			
			//打包EMV数据
			
			List<byte[]> tags = new ArrayList<byte[]>();
			tags.add(EmvTagDef.EMV_TAG_9F26_IC_AC );
			tags.add(EmvTagDef.EMV_TAG_9F27_IC_CID );
			tags.add(EmvTagDef.EMV_TAG_9F10_IC_ISSAPPDATA );
			tags.add(EmvTagDef.EMV_TAG_9F37_TM_UNPNUM );			
			/*
			 *tag省略 。。。。。。。
			 */
			
			GetEmvDataResult getREmvDataResult = Controler.GetEmvData(tags);
			
			if(!getREmvDataResult.commResult.equals(CommEnum.COMMRET.NOERROR))
			{
				Actions.ShowBlockMsg("提示", "获取EMV数据错误", "", "确定");
				//错误要记得中止EMV
				Controler.EndEmv();
				return;
			}
			
			/*
			 * 根据getREmvDataResult，inputpinResult的数据组8583包，此处省略，假定为byte8583
			 */			
			byte[] byte8583 = new byte[512];
			
			for(int i = 0; i < 15; ++ i)
			{
				byte8583[i] = (byte)i;
			}
			int actual8583Len = 15;
			
			
			Actions.ShowUnBlockMsg("提示", "正在计算mac", "", null);
			CalMacResult macResult = Controler.CalMac(CommEnum.MACALG.EBC, byte8583,actual8583Len);
			if(!macResult.commResult.equals(CommEnum.COMMRET.NOERROR))
			{
				Actions.ShowBlockMsg("提示", "mac计算错误", "", "确定");
				//错误要记得中止EMV
				Controler.EndEmv();
				return;
			}
			
			/*
			 * 与POSP通讯，此处省略，假定返回的39域为00,55域未rspField55
			 */
			String rspField39 = "00";
			byte[] rspField55 = new byte[256];
			int rspField55Len = 0;
			
			/* 进行二次授权。实际交易需要，demo为了成功提示，屏蔽
			EmvDealOnlineRspResult emvDealResult = BluetoothControler.EmvDealOnlineRsp(rspField39.equals("00"), rspField55,rspField55Len);
			if(!emvDealResult.commResult.equals(BluetoothCommEnum.BLUETOOTHCOMMRET.NOERROR)
				|| !emvDealResult.authResult.equals(BluetoothCommEnum.EMVDEALONLINERSP.SUCC))
			{
				Actions.ShowBlockMsg("提示", "二次授权失败", "", "确定");
				//错误要记得中止EMV
				BluetoothControler.EndEmv();
				return;
			}
			*/
			
			Actions.ShowBlockMsg("提示", "卡片余额为：1000", "", "确定");
			Controler.EndEmv();
		}
	}
}
